using System;
using System.Collections.Generic;

using UnityEngine;

public class ShipData {
	private float totalWeight;
	private float totalCost;
	private int totalParts;
	private int maxStage;
	//private bool debug;
	List<StageData> shipInfo;
	
	//values from the unofficial wiki
	public const float KerbinGravity = 9.8068f; 
	public const float MunGravity = 1.628f;
	public const float MinMusGravity = 0.785f;

	public float TotalWeight {
		get {
			calculateTotals ();
			return totalWeight;
		}
	}

	public float TotalCost {
		get {
			calculateTotals ();
			return totalCost;
		}
	}

	public int TotalParts {
		get {
			calculateTotals ();
			return totalParts;
		}
	}

	public ShipData () {
		totalParts = 0;
		totalWeight = 0;
		totalCost = 0;
		maxStage = -1;
		shipInfo = new List<StageData> ();
	}
		
	public void ClearData () {
		totalParts = 0;
		totalWeight = 0;
		totalCost = 0;
		maxStage = -1;
		foreach (StageData s in shipInfo) {
			s.zero ();
		}
	}

	public int Count () {
		return maxStage + 1;
	}

	public void calculateTotals () {
		//Debug.Log("Shipdata calculateTotals");
		totalWeight = 0;
		totalCost = 0;
		totalParts = 0;
		
		for (int i = 0; i < Count(); i++) {
			StageData stage = this.getStage (i);
			totalWeight += stage.mass;
			totalCost += stage.cost;
			stage.totalWeight = totalWeight;
			stage.totalCost = totalCost;
			totalParts += stage.parts;
		}			
	}

	public StageData getStage (int stage) {
		//Debug.Log("Shipdata getstage: "+ stage);
		//store highest stage
		if (maxStage < stage)
			maxStage = stage;
		//make sure list contains this stage
		while (shipInfo.Count <= stage) {
			shipInfo.Add (new StageData ());
		}
			
		return shipInfo [stage];
	}

}

